package com.weactivity.service.impl;

import com.weactivity.dto.ApplyExpenseDTO;
import com.weactivity.dto.ApproveDTO;
import com.weactivity.dto.BudgetDTO;
import com.weactivity.entity.Budget;
import com.weactivity.entity.Expense;
import com.weactivity.mapper.BudgetMapper;
import com.weactivity.service.BudgetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BudgetServiceImpl implements BudgetService {
    @Autowired
    private BudgetMapper budgetMapper;


    /**
     * 添加预算
     *
     * @param budgetDTO
     * @return
     */
    @Override
    public void add(BudgetDTO budgetDTO) {
        Budget budget = new Budget();
        BeanUtils.copyProperties(budgetDTO,budget);
        double temp = budgetDTO.getTotalBudget();
        budget.setRemain(temp);
        budgetMapper.add(budget);

    }

    //查询预算
    @Override
    public double findBudget(int aid) {
        return budgetMapper.findBudget(aid);
    }
    //查询剩余预算
    @Override
    public double findRemain(int aid) {
        return budgetMapper.findRemain(aid);
    }

    /**
     * 修改预算
     *
     * @param newBudget,newremain
     * @param aid
     * @return
     */
    @Override
    public void updateBudget(double newBudget, double newRemain, int aid) {
        Budget budget = new Budget();
        budget.setRemain(newRemain);
        budget.setTotalBudget(newBudget);
        budget.setAid(aid);
        budgetMapper.updateBudget(budget);
    }
    /**
     * 展示活动预算
     *
     * @param
     * @return
     */
    @Override
    public List<Budget> showAll() {
        return budgetMapper.showAll();
    }

    /**
     * 申请报销
     *
     * @param applyExpenseDTO
     * @return
     */
    @Override
    public void apply(ApplyExpenseDTO applyExpenseDTO) {
        Expense expense = new Expense();
        BeanUtils.copyProperties(applyExpenseDTO,expense);
        expense.setApprove(0);
        expense.setState(0);
        budgetMapper.apply(expense);
    }

    /**
     * 展示申请
     *
     *
     * @param state
     * @return
     */
    @Override
    public List<Expense> showApply(int state) {
        return budgetMapper.showApply(state);
    }

    /**
     * 处理拒绝申请
     *
     *
     * @param approveDTO
     *
     * @return
     */
    @Override
    public void dispose1(ApproveDTO approveDTO) {
        Expense expense = new Expense();
        expense.setAid(approveDTO.getAid());
        expense.setUid(approveDTO.getUid());
        expense.setApprove(approveDTO.getApprove());
        budgetMapper.updateState(expense);
    }

    /**
     * 处理同意申请
     *
     *
     * @param uid
     * @param aid
     * @param approve
     * @param remain
     * @return
     */
    @Override
    public void dispose2(int uid, int aid,int approve,double remain) {
        Expense expense = new Expense();
        expense.setApprove(approve);
        expense.setAid(aid);
        expense.setUid(uid);
        Budget budget = new Budget();
        budget.setRemain(remain);
        budget.setAid(aid);
        budgetMapper.updateState(expense);
        budgetMapper.dispose(budget);

    }
    //查找报销金额
    @Override
    public double findMoney(ApproveDTO approveDTO) {
        return budgetMapper.findMoney(approveDTO);
    }




}
